﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YTCMS.Core;
using YTCMS.Domain.Entities;
using YTCMS.Services.Services;

namespace Admin.Controllers
{
    public class ProductController : BaseController
    {
        private readonly IproductsService _product;
        private readonly IimagesService _images;
        //
        // GET: /Product/
        public ProductController(IproductsService product, IimagesService images)
        {
            this._product = product;
            this._images = images;
        }
        //
        // GET: /Product/
        public ActionResult Index(int? pageIndex = 1, int? pageSize = 10)
        {
            int pcount = 0;
            var model = _product.PageList(s => true, pageIndex.Value - 1, pageSize.Value, out pcount);
            ViewBag.PageSize = pageSize.Value;
            ViewBag.TotalCount = pcount;
            return View(model);
        }
        public ActionResult Search(string pname, int? pageIndex = 1, int? pageSize = 10)
        {
            int pcount = 0;
            var model = _product.GetMany(s => true);
            model = QueryHelper.WhereIf<products>(model, s => s.Name.Contains(pname), !string.IsNullOrWhiteSpace(pname));
            model = _product.PageList(model, pageIndex.Value - 1, pageSize.Value, out pcount);
            ViewBag.PageSize = pageSize.Value;
            ViewBag.TotalCount = pcount;
            return PartialView("List", model);
        }
        public ActionResult Add()
        {
            return View();
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Add(products products)
        {
            products.CreateTime = System.DateTime.Now;
            products.EditTime = System.DateTime.Now;
            products.State = (int)CommonState.正常;
            products.uid = current.Id;
            products.CategoryId = Request["CategoryId"].StrConvertInt();
            if (ModelState.IsValid)
            {
                var ddl1 = Request["ddl1"];
                var ddl2 = Request["ddl2"];
                string sb = "";
                if (!string.IsNullOrEmpty(ddl1))
                    sb += "'" + ddl1 + "',";
                if (!string.IsNullOrEmpty(ddl2))
                    sb += "'" + ddl2 + "',";
              
                if (!string.IsNullOrEmpty(sb))
                    sb = sb.Remove(sb.Length - 1, 1);
                if (sb != "")
                {
                    products.strcategory = sb;
                }
                var images = Request["hidimg"];
                products.Image = images != null? images.Split(',')[0]:"";
                var p = _product.Add(products);
                if (!string.IsNullOrEmpty(images))
                {
                    var imgs = images.Split(',');
                    for (int i = 0; i < imgs.Length; i++)
                    {
                        _images.Add(new images() { Pid = p.Id, State = (int)CommonState.正常, Url = imgs[i], CreateTime = System.DateTime.Now, Type = (int)Images.线路 });
                    }
                }
               
                return RedirectToAction("Index");
            }
            return View();
        }

        public ActionResult Edit(int id)
        {
            var model = _product.Get(s => s.Id == id);
            ViewData["pimgs"] = _images.GetMany(s => s.Pid == id && s.Type == (int)Images.线路).Select(s => s.Url).ToArray();
            return View(model);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(products product)
        {
            product.EditTime = System.DateTime.Now;

            if (ModelState.IsValid)
            {
                var ddl1 = Request["ddl1"];
                if (!string.IsNullOrEmpty(ddl1))
                {
                    var ddl2 = Request["ddl2"];
                    string sb = "";
                    if (!string.IsNullOrEmpty(ddl1))
                        sb += "'" + ddl1 + "',";
                    if (!string.IsNullOrEmpty(ddl2))
                        sb += "'" + ddl2 + "',";
                    if (!string.IsNullOrEmpty(sb))
                        sb = sb.Remove(sb.Length - 1, 1);
                    if (sb != "")
                    {
                        product.strcategory = sb;
                    }
                }

                var images = Request["hidimg"];
               

 

                if (!string.IsNullOrEmpty(images))
                {
                    product.Image = images.Split(',')[0];
                    var hid = product.Id;
                    _images.Delete(s => s.Pid == hid && s.Type == (int)Images.线路);
                    var imgs = images.Split(',');
                    for (int i = 0; i < imgs.Length; i++)
                    {
                        _images.Add(new images() { Pid = hid, State = (int)CommonState.正常, Url = imgs[i], CreateTime = System.DateTime.Now, Type = (int)Images.线路 });
                    }
                }

                _product.Update(product);
                return RedirectToAction("Index");
            }
            return View();
        }

        public JsonResult Delete(int id)
        {
            var model = _product.Get(s => s.Id == id);
            _product.Delete(model);
            return new JsonResult() { Data = true, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }
        public JsonResult Deletes(string ids)
        {
            string[] idsarry = ids.Split(',');
            _product.Delete(DeleteExpression.DeleteAll<products, string>(s => s.Id.ToString(), idsarry));
            return new JsonResult() { Data = true, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }

	}
}